/**
 * 
 */
package com.novartis.nims.trialsubmitter;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.net.URI;

/**
 * @author SNYDEJO1
 *
 */
public class TrialSubmitter implements PropertyChangeListener {

	private PropertyChangeSupport mPropertyChangeSupport;
	private TrialSubmitterController mController = null;
	
	private ApplicationConfiguration mConfigurationProperties;
	
	private URI mInputLocation;
	private URI mOutputLocation;
	
	private int mTrialSubmissionNumber = 1;
	
	private boolean mSort = false;
	private boolean mClean = false;
	private boolean mMakeDescriptor = false;

	public TrialSubmitter(ApplicationConfiguration config, URI input, URI output) {
		mConfigurationProperties = config;
		mInputLocation = input;
		mOutputLocation = output;
		mPropertyChangeSupport = new PropertyChangeSupport(this);
	}
	
	public void setController(TrialSubmitterController controller) {
		mController = controller;
		mPropertyChangeSupport = mController.getPropertyChangeSupport();
	}
	
	public void setSubmissionNumber(int number) {
		mTrialSubmissionNumber = number;
	}
	
	public void setSort(boolean sort) {
		this.mSort = sort;
	}
	
	public void setClean(boolean clean) {
		this.mClean  = clean;
	}
	
	public void setMakeDescriptor(boolean makeDescriptor) {
		this.mMakeDescriptor  = makeDescriptor;
	}
	
	public void execute() throws TrialReaderException, OdmSinkException, AnonymizerFilterException {
		Trial trial = null;
		
		
		TrialReader reader = null;
		if (mSort) {
			reader = new UnsortedTrialReader(this, mInputLocation);
		} else {
			reader = new SortedTrialReader(this, mInputLocation);
		}
		reader.setExpectNonDicom(mConfigurationProperties.getExpectNonDicom());
		reader.setAbortOnFileMissingFileKey(mConfigurationProperties.getAbortOnFileMissingFileKey());
		reader.setAbortOnFileMissingScanKey(mConfigurationProperties.getAbortOnFileMissingScanKey());
		reader.setAbortOnFileMissingScanName(mConfigurationProperties.getAbortOnFileMissingScanName());
		reader.setAbortOnFileMissingVisitKey(mConfigurationProperties.getAbortOnFileMissingVisitKey());
		reader.setAbortOnFileWithDuplicateFileKey(mConfigurationProperties.getAbortOnFileWithDuplicateFileKey());
		reader.setAbortOnUnspecifiedFileError(mConfigurationProperties.getAbortOnUnspecifiedFileError());
		reader.setAllowBadPixelData(mConfigurationProperties.getAllowBadPixelData());
		reader.setFileKey(mConfigurationProperties.getFileKey());
		reader.setScanKey(mConfigurationProperties.getScanKey());
		reader.setScanName(mConfigurationProperties.getScanName());
		reader.setVisitKey(mConfigurationProperties.getVisitKey());
		reader.setVisitNameSeparator(mConfigurationProperties.getVisitNameSeparator());
		reader.setUnscheduledVisitIdentifier(mConfigurationProperties.getUnscheduledVisitIdentifier());
		reader.setUnscheduledVisitNumber(mConfigurationProperties.getUnscheduledVisitNumber());
		reader.setVisitsOrganized(mConfigurationProperties.getVisitsOrganized());
		reader.setScansOrganized(mConfigurationProperties.getScansOrganized());
		reader.addPropertyChangeLister(this);
		

		trial = reader.execute();
		if (this.isCancelled()) return;
		
		

		AnonymizerFilter anonymizer = new AnonymizerFilter(this, mOutputLocation);
		anonymizer.setAbortOnUnspecifiedFileError(mConfigurationProperties.getAbortOnUnspecifiedFileError());
		anonymizer.setTrial(trial);
		anonymizer.setCopyOnly(! mClean);
		anonymizer.setAllowBadPixelData(mConfigurationProperties.getAllowBadPixelData());
		anonymizer.setVisitNameSeparator(mConfigurationProperties.getVisitNameSeparator());
		anonymizer.addPropertyChangeLister(this);
		
		if (mSort || mClean) {
			anonymizer.execute();
			if (this.isCancelled()) return;
		}
		
		
		

		if (mMakeDescriptor) {
			OdmSink sink = new OdmSink(this, mOutputLocation, String.valueOf(mTrialSubmissionNumber),
					mConfigurationProperties.getCROName(), mConfigurationProperties.getExtensionNumber());
			sink.setTrial(trial);
			sink.addPropertyChangeLister(this);
			
			sink.execute();
		}
	}

	boolean isCancelled() {
		return (mController == null) ? false : mController.isCancelled();
	}

	public void propertyChange(PropertyChangeEvent event) {
		mPropertyChangeSupport.firePropertyChange(event);
	}

}
